package com.rd.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import static java.util.TimeZone.*;

/**
 * 自定义日期格式化工具类
 */
public class CustomDateTimeSerializer extends JsonSerializer<Date> {

    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        // 创建简单日期格式化器
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 设置时区
        sdf.setTimeZone(getTimeZone("Asia/Shanghai"));
        //将Date---->String .使用json工具写字符串
        jsonGenerator.writeString(sdf.format(date));
    }
}
